package com.kgc.scd.controller;

import com.alibaba.fastjson.JSONObject;
import com.kgc.scd.annotation.RequestShopUser;
import com.kgc.scd.annotation.TokenPermission;
import com.kgc.scd.bean.ShopUser;
import com.kgc.scd.config.UserCollectConfig;
import com.kgc.scd.service.ShareWebService;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * Created On : 2022/11/17.
 * <p>
 * Author : yantao
 * <p>
 * Description: ShareFriendsController
 */
@Slf4j
@RestController
@RequestMapping("/yt")
@Api(tags = "12、分享好友接口(yt)",value = "分享商品对应的链接给好友，好友查看并支付成功后可以给分享者添加积分")
public class ShareFriendsController {
    @Autowired
    private UserCollectConfig userCollectConfig;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private ShareWebService shareWebService;

    /**
     * @author : yantao
     * @date   : 2022/11/17
     * @param  : [shopUser,goodsId]
     * @return : com.kgc.scd.util.RequestResult<?>
     * @description : 用户获取生成分享码
     */
    //Generate invitation code
    @GetMapping("getInvitationCode")
    @TokenPermission
    @ApiOperation(value = "用户获取生成分享码", notes = "返回,分享链接,需要参数商品id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 999,message = "失败")
    })
    public RequestResult<?> getInvitationCode(@ApiIgnore @RequestShopUser ShopUser shopUser,
                                              @ApiParam(value = "商品id") @RequestParam String goodsId) {



        return ResultBuildUtil.success( shareWebService.generateShareCode(shopUser.getId(),goodsId));
    }

    /**
     * @author : yantao
     * @date   : 2022/11/17
     * @param  : [shopUser, goodsId]
     * @return : com.kgc.scd.util.RequestResult<?>
     * @description : 其他用户点击分享链接
     */
    @GetMapping("clickShareLink")
    @TokenPermission
    @ApiOperation(value = "其他用户点击分享链接", notes = "其它用户点击分享链接的操作，达到分享成功的效果")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 999,message = "失败")
    })
    public RequestResult<?> clickShareLink(@ApiIgnore @RequestShopUser ShopUser shopUser,
                                           @ApiParam(value = "状态码") @RequestParam String code) {

        if (redisUtils.get(userCollectConfig.getUserCodeCorrespondShare()+":"+code)==null){
            return ResultBuildUtil.fail("203","验证码无效",null);
        }

        JSONObject json = JSONObject.parseObject(redisUtils.get(userCollectConfig.getUserCodeCorrespondShare()+":"+code).toString());
        Map<String, Object> map = json;
        String userId = map.get("userId").toString();
        String goodsId= map.get("goodsId").toString();



        if (shopUser.getId().equals(userId)){
            return ResultBuildUtil.fail("201","用户自己点击链接",null);
        }

        //TODO 调用前往商品详细接口

        String a= shareWebService.judgeLinkageOvertime(userId,goodsId,code,shopUser.getId());
        if (!ObjectUtils.isEmpty(a)){
            return ResultBuildUtil.fail("202",a,null);
        }




        shareWebService.userBindCode(shopUser.getId(), goodsId, code,userId);
        return ResultBuildUtil.success("http://localhost:8099/shopCommentList?goodsId="+goodsId+"&pageNo=1&pageSize=3");
    }


    /**
     * @author : yantao
     * @date   : 2022/11/17
     * @param  : [shopUser, goodsId]
     * @return : com.kgc.scd.util.RequestResult<?>
     * @description : 用户购买后,修改状态
     */
    @GetMapping("addIntegral")
    @TokenPermission
    @ApiOperation(value = "给通过的用户加积分", notes = ",需要参数商品id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 999,message = "失败")
    })
    public RequestResult<?> addIntegral(@ApiIgnore @RequestShopUser ShopUser shopUser,
                                       @ApiParam(value = "商品id") @RequestParam String goodsId) {

        shareWebService.userAddIntegral(shopUser.getId(),goodsId);
        return ResultBuildUtil.success();

    }

}
